# -*- coding: utf-8 -*-

from trac.ticket.model import Ticket
from trac.resource import ResourceNotFound
from trac.util.compat import set
from genshi.builder import tag

from model import Mail

def linkify_ids(env, req, ids):
    data = []
    for id in sorted(ids, key=lambda x: int(x)):
        try:
            mail = Mail(env, id)
            data.append(tag.a('mail:%s'%mail.id, href=req.href.mailarchive(mail.id), class_='ticket', title=mail.subject))
        except ResourceNotFound:
            data.append('%s'%id)
        data.append(', ')
    if data:
        del data[-1] # Remove the last comma if needed
    return tag.span(*data)

